<!-- <h1>编辑资料</h1> -->
<script setup lang='ts'>
import { reactive, ref } from "vue";
import type { FormInstance, FormRules, ElMessage } from "element-plus";
import { Plus } from "@element-plus/icons-vue";
import type { UploadProps } from "element-plus";

const formSize = ref("default");
const ruleFormRef = ref<FormInstance>();
const ruleForm = reactive({
  name: "",
  region: "",
  count: "",
  date1: "",
  date2: "",
  delivery: false,
  type: [],
  resource: "",
  desc: "",
});

const rules = reactive<FormRules>({
  name: [
    { required: true, message: "Please input Activity name", trigger: "blur" },
    { min: 3, max: 5, message: "Length should be 3 to 5", trigger: "blur" },
  ],
  region: [
    {
      required: true,
      message: "Please select Activity zone",
      trigger: "change",
    },
  ],
  count: [
    {
      required: true,
      message: "Please select Activity count",
      trigger: "change",
    },
  ],
  date1: [
    {
      type: "date",
      required: true,
      message: "Please pick a date",
      trigger: "change",
    },
  ],
  date2: [
    {
      type: "date",
      required: true,
      message: "Please pick a time",
      trigger: "change",
    },
  ],
  type: [
    {
      type: "array",
      required: true,
      message: "Please select at least one activity type",
      trigger: "change",
    },
  ],
  resource: [
    {
      required: true,
      message: "Please select activity resource",
      trigger: "change",
    },
  ],
  desc: [
    { required: true, message: "Please input activity form", trigger: "blur" },
  ],
});

const submitForm = async (formEl: FormInstance | undefined) => {
  if (!formEl) return;
  await formEl.validate((valid, fields) => {
    if (valid) {
      console.log("submit!");
    } else {
      console.log("error submit!", fields);
    }
  });
};

const resetForm = (formEl: FormInstance | undefined) => {
  if (!formEl) return;
  formEl.resetFields();
};

const options = Array.from({ length: 10000 }).map((_, idx) => ({
  value: `${idx + 1}`,
  label: `${idx + 1}`,
}));

// 上传头像
const imageUrl = ref("");

const handleAvatarSuccess: UploadProps["onSuccess"] = (
  response,
  uploadFile
) => {
  imageUrl.value = URL.createObjectURL(uploadFile.raw!);
};

const beforeAvatarUpload: UploadProps["beforeUpload"] = (rawFile) => {
  if (rawFile.type !== "image/jpeg") {
    ElMessage.error("Avatar picture must be JPG format!");
    return false;
  } else if (rawFile.size / 1024 / 1024 > 2) {
    ElMessage.error("Avatar picture size can not exceed 2MB!");
    return false;
  }
  return true;
};
</script>
<!-- 模型 -->
<template>
  <div class="">
    <div>
      <el-row>
        <el-col :span="20"><div class="">编辑资料</div></el-col>
        <el-col :span="4"
          ><el-button plain class=""> &lt; 返回</el-button></el-col
        >
      </el-row>
    </div>
    <!-- 表单 -->
    <el-form
      ref="ruleFormRef"
      :model="ruleForm"
      :rules="rules"
      label-width="200px"
      class="demo-ruleForm"
      :size="formSize"
      status-icon
    >
      <!-- 上传头像 -->
      <el-upload
        class="avatar-uploader"
        action="https://run.mocky.io/v3/9d059bf9-4660-45f2-925d-ce80ad6c4d15"
        :show-file-list="false"
        :on-success="handleAvatarSuccess"
        :before-upload="beforeAvatarUpload"
      >
        <img v-if="imageUrl" :src="imageUrl" class="avatar" />
        <el-icon v-else class="avatar-uploader-icon"><Plus /></el-icon>
      </el-upload>
      <el-form-item label="会员等级" prop="region">
        <el-select v-model="ruleForm.region" placeholder="会员等级">
          <el-option label="一般会员" value="shanghai" />
          <el-option label="超级会员" value="beijing" />
        </el-select>
      </el-form-item>
      <el-form-item label="姓名" prop="name">
        <el-col :span="6">
          <el-input v-model="ruleForm.name" />
        </el-col>
      </el-form-item>
      <el-form-item label="性别" prop="resource">
        <el-radio-group v-model="ruleForm.resource">
          <el-radio label="男" />
          <el-radio label="女" />
        </el-radio-group>
      </el-form-item>
      <el-form-item label="手机" prop="name">
        <el-col :span="6">
          <el-input v-model="ruleForm.name" placeholder="请输入手机号" />
        </el-col>
      </el-form-item>
      <el-form-item label="微信" prop="name" :required="false">
        <el-col :span="6">
          <el-input v-model="ruleForm.name" placeholder="请输入微信号" />
        </el-col>
      </el-form-item>
      <el-form-item label="昵称" prop="name" :required="false">
        <el-col :span="6">
          <el-input v-model="ruleForm.name" placeholder="" />
        </el-col>
      </el-form-item>

      <el-form-item label="生日" :required="false">
        <el-form-item prop="date1">
          <el-date-picker
            v-model="ruleForm.date1"
            type="date"
            label="Pick a date"
            placeholder=""
            size="Default"
          />
        </el-form-item>
      </el-form-item>
      <el-form-item label="身份证" prop="name" :required="false">
        <el-col :span="6">
          <el-input v-model="ruleForm.name" placeholder="请填写身份证号码" />
        </el-col>
      </el-form-item>
      <el-form-item label="城市" prop="region" :required="false">
        <el-col :span="4">
          <el-select v-model="ruleForm.region" placeholder="">
            <el-option label="一般会员" value="shanghai" />
            <el-option label="超级会员" value="beijing" />
          </el-select>
        </el-col>
        <el-col :span="6">
          <el-select v-model="ruleForm.region" placeholder="">
            <el-option label="一般会员" value="shanghai" />
            <el-option label="超级会员" value="beijing" />
          </el-select>
        </el-col>
      </el-form-item>
      <el-form-item label="职业" prop="name" :required="false">
        <el-col :span="6">
          <el-input v-model="ruleForm.name" placeholder="" />
        </el-col>
      </el-form-item>
      <el-form-item label="身高" prop="name" :required="false">
        <el-col :span="6">
          <el-input v-model="ruleForm.name" placeholder="" />
        </el-col>
      </el-form-item>
      <el-form-item label="体重" prop="name" :required="false">
        <el-col :span="6">
          <el-input v-model="ruleForm.name" placeholder="" />
        </el-col>
      </el-form-item>
      <el-form-item label="个性签名" prop="name" :required="false">
        <el-col :span="6">
          <el-input
            v-model="ruleForm.name"
            placeholder="请填写个性签名（20个字以内）"
          />
        </el-col>
      </el-form-item>
      <el-form-item label="健身目的" prop="region" :required="false">
      </el-form-item>
      <el-form-item label="选择教练" prop="region" :required="false">
        <el-select v-model="ruleForm.region" placeholder="会员等级">
          <el-option label="一般会员" value="shanghai" />
          <el-option label="超级会员" value="beijing" />
        </el-select>
      </el-form-item>
      <el-form-item label="所属场馆" prop="resource">
        <el-radio-group v-model="ruleForm.resource">
          <el-radio label="男" />
          <el-radio label="女" />
        </el-radio-group>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" @click="submitForm(ruleFormRef)">
          提交
        </el-button>
        <el-button @click="resetForm(ruleFormRef)">重制</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<style scoped lang="less">
.avatar-uploader .avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>